package com.ssm.crud.service;


import com.ssm.crud.bean.Employee;
import com.ssm.crud.bean.EmployeeExample;
import com.ssm.crud.bean.Msg;
import com.ssm.crud.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    /**
     *校验用户名是否可用
     * true:有数据
     * false：小于0，没有数据
     */
    public boolean  checkUser(String empName) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);

        return count == 0;
    }

    /*
    * 查询所有员工数据（分页查询）
    * */
    public List<Employee> getAll(){
        //这不是一个分页查询
        return employeeMapper.selectByExampleWithDept(null);
    }

    /**
     * 员工保存方法
     * @param employee
     */
    public void SaveEmp(Employee employee){
        int i = employeeMapper.insertSelective(employee);
    }

    public Employee getEmp(String id){
       Employee employee = employeeMapper.selectByPrimaryKey(new Integer(id));

       return employee;
    }
    /**
     * 员工更新方法
     * @param employee
     */
    public void updateEmp(Employee employee){
        int i = employeeMapper.updateByPrimaryKey(employee);
    }

    //删除员工的方法
    public void deleteEmpById(Integer id){
        employeeMapper.deleteByPrimaryKey(id);
    }

    //批量删除员工的方法
    public void deleteBatch(List<Integer> ids){
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        //delete from xxx where emp_id in (1,2,3,4,5)
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
    }
}
